<?php

namespace app\data\command;

use app\data\service\AliSingleCallService;
use think\admin\Command;
use think\admin\Exception;
use think\console\Input;
use think\console\Output;
use think\facade\Log;

/**
 * 未回复消息通知
 * Class NotReplyRecordNotice
 * @package app\data\command
 */
class NotReplyRecordNotify extends Command
{
    protected function configure()
    {
        $this->setName('not_reply_record_notify');
        $this->setDescription('未回复消息通知(15/30分钟)');
    }

    /**
     * 业务指令执行
     * @param Input $input
     * @param Output $output
     * @return void
     * @throws Exception
     */
    protected function execute(Input $input, Output $output)
    {
        while (true) {

            // 30分钟后未回复，给对接人拨打一个语音电话通知
            $min30 = date('Y-m-d H:i:s', time() - (60 * 30));
            foreach ($this->app->db->name('data_record')->field('id,uuid_docking,uuid_city,uuid_docking_notice,uuid_city_notice,time_docking,plate_number')
                         ->whereIn('paint_status', [-1, 0])
                         ->where('uuid_docking_notice', 0)
                         ->where('time_docking', '<', $min30)
                         ->whereNull('time_invite')
                         ->cursor() as $vo) try {

                $mobile = $this->app->db->name('work_user')->where('id', $vo['uuid_docking'])->value('contact_phone');
                if (!empty($mobile)) {
                    AliSingleCallService::singleCallByTts($mobile, 'TTS_287030076', ['plateNumber' => $vo['plate_number']]);
                }
                $this->app->db->name('data_record')->where('id', $vo['id'])->update(['uuid_docking_notice' => 1]);
            }  catch (\Exception $exception) {
                Log::error('未回复消息通知30分钟-报错: ' . $exception->getMessage());
            }

            // 15分钟后未回复，给施工师傅拨打一个语音电话通知
            $min15 = date('Y-m-d H:i:s', time() - (60 * 15));
            foreach ($this->app->db->name('data_record')->field('id,uuid_docking,uuid_city,uuid_docking_notice,uuid_city_notice,time_docking,plate_number')
                         ->whereIn('paint_status', [-1, 0])
                         ->where('uuid_city_notice', 0)
                         ->where('time_docking', '<', $min15)
                         ->whereNull('time_invite')
                         ->cursor() as $vo) try {

                $mobile = $this->app->db->name('work_user')->where('id', $vo['uuid_city'])->value('contact_phone');
                if (!empty($mobile)) {
                    AliSingleCallService::singleCallByTts($mobile, 'TTS_287015059', []);
                }
                $this->app->db->name('data_record')->where('id', $vo['id'])->update(['uuid_city_notice' => 1]);
            }  catch (\Exception $exception) {
                Log::error('未回复消息通知15分钟-报错: ' . $exception->getMessage());
            }


            sleep(30);
        }
    }




}